//
//  FontSelector.m
//  BehindTheMask
//
//  Created by Basuke on 08/03/12.
//  Copyright 2008 Basuke's. All rights reserved.
//

#import "FontSelector.h"
#import "Console.h"

@implementation FontSelector

@synthesize fonts;

+ (FontSelector *)fontSelector {
	FontSelectorMainViewController *main = [[[FontSelectorMainViewController alloc] init] autorelease];
	FontSelector *selector = [[[FontSelector alloc] initWithRootViewController:main] autorelease];
	
	main.fontSelector = selector;
	
	return selector;
}

- (id)init
{
	if (self = [super init]) {
		// Initialize your view controller.
		self.title = @"Fonts";
		self.toolbarItem.image = [UIImage imageNamed:@"Fonts.png"];
		
		self.fonts = [UIFont familyNames];
	}
	return self;
}


- (void)loadView
{
	// setup the parent content view to host the UITableView
	UIView *contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	[contentView setBackgroundColor:[UIColor whiteColor]];
	self.view = contentView;
	[contentView release];
	
	// setup our content view so that it auto-rotates along with the UViewController
	self.view.autoresizesSubviews = YES;
	self.view.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
	
	UINavigationItem *navItem = self.navigationItem;
	
	// Add the "Style" button to the navigation bar (help set the bar's style or color)
	UIButton *button = [UIButton buttonWithType:UIButtonTypeNavigation];
	[button setTitle:@"Style" forStates:UIControlStateNormal];
	[button addTarget:self action:@selector(styleAction:) forControlEvents:UIControlEventTouchUpInside];
	navItem.customLeftView = button;
	
}

- (void)styleAction:(id)sender
{
	UIActionSheet *styleAlert =
	[[UIActionSheet alloc] initWithTitle:@"UIBarStyle" message:@"Pick the style for your navigation bar:"
								delegate:self defaultButton:nil cancelButton:nil
							otherButtons:@"Default", @"BlackOpaque", @"BlackTranslucent", @"Cancel", nil];
	[styleAlert showInView:self.view];
	[styleAlert release];
}

- (void)modalView:(UIModalView *)modalView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	NSLog(@"%d", buttonIndex);
	switch (buttonIndex)
 {
	 case 0:
		 self.navigationController.navigationBarStyle = UIBarStyleDefault;
		 break;
	 case 1:
		 self.navigationController.navigationBarStyle = UIBarStyleBlackOpaque;
		 break;
	 case 2:
		 self.navigationController.navigationBarStyle = UIBarStyleBlackTranslucent;
		 break;
 }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	// Return YES for supported orientations.
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning
{
	[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview.
	// Release anything that's not essential, such as cached data.
}

- (void)dealloc
{
	[super dealloc];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [self.fonts count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath withAvailableCell:(UITableViewCell *)availableCell {
	UISimpleTableViewCell *cell = nil;
	if (availableCell != nil) {
		cell = (UISimpleTableViewCell *)availableCell;
	} else {
		CGRect frame = CGRectMake(0, 0, 300, 44);
		cell = [[[UISimpleTableViewCell alloc] initWithFrame:frame] autorelease];
	}
	NSString *familyName = [self.fonts objectAtIndex:[indexPath row]];
	cell.font = [UIFont fontWithName:familyName size:16];
	cell.text = cell.font.familyName;
	
	if ([[UIFont fontNamesForFamilyName:familyName] count] > 1) {
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	} else {
		cell.accessoryType = UITableViewCellAccessoryNone;
	}
	return cell;
}

- (void)tableView:(UITableView *)tableView selectionDidChangeToIndexPath:(NSIndexPath *)newIndexPath fromIndexPath:(NSIndexPath *)oldIndexPath {
	NSString *familyName = [self.fonts objectAtIndex:[newIndexPath indexAtPosition:1]];
	[[Console sharedConsole] log:[NSString stringWithFormat: @"%@ selected", familyName]];
	
	NSArray *family = [UIFont fontNamesForFamilyName:familyName];
	if ([family count] > 1) {
		
	}
	
	NSLog(@"length: %d [0]: %d, [1]: %d", [newIndexPath length], [newIndexPath indexAtPosition:0], [newIndexPath indexAtPosition:1]);
}


@end

@implementation FontSelectorMainViewController

@synthesize fontSelector;

- (id)init {
	if (self = [super init]) {
		if (self = [super init]) {
			// Initialize your view controller.
			self.title = @"Fonts Main";
			self.toolbarItem.image = [UIImage imageNamed:@"Fonts.png"];
		}
		return self;
	}
	
	return self;
}

- (void)loadView
{
	// setup the parent content view to host the UITableView
	UIView *contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	[contentView setBackgroundColor:[UIColor whiteColor]];
	self.view = contentView;
	[contentView release];
	
	// setup our content view so that it auto-rotates along with the UViewController
	self.view.autoresizesSubviews = YES;
	self.view.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
	
	UINavigationItem *navItem = self.navigationItem;
	
	// Add the "Style" button to the navigation bar (help set the bar's style or color)
	UIButton *button = [UIButton buttonWithType:UIButtonTypeNavigation];
	[button setTitle:@"Style" forStates:UIControlStateNormal];
	[button addTarget:self action:@selector(styleAction:) forControlEvents:UIControlEventTouchUpInside];
	navItem.customLeftView = button;
	
}

@end

@implementation FontSelectorSubViewController

@synthesize fontSelector;

- (id)init {
	if (self = [super init]) {
		
	}
	
	return self;
}

@end
